package com.hardydou.system.tasks;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.quartz.*;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * @author ：hardy
 * @description：任务
 * @date ：Created in 2020/6/30
 * @Copyright ：Hardydou.com
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class Task extends QuartzJobBean {

    private Log log = LogFactory.get(Task.class);

    private JobExecutionContext context;

    @Override
    public void executeInternal(JobExecutionContext context) throws JobExecutionException {
        this.context = context;
        log.info("Job execute params({}) !", getInvokeParam());
        execute();
    }

    public void execute() {
        log.info("You should override the method({})  ", this.getClass().getName() + ".execute()");
    }

    protected JobExecutionContext getContext() {
        return context;
    }

    /***
     * 获取 任务参数
     * @return
     */
    protected JobDataMap getJobDataMap() {
        return context.getTrigger().getJobDataMap();
    }

    /***
     * 获取参数值
     * @param param
     * @param <T>
     * @return
     */
    protected <T> T get(String param) {
        return (T) getJobDataMap().get(param);
    }

    protected String getInvokeParam() {
        return get("invokeParam");
    }

}
